﻿using DataSyncCommon;
using Quartz;
using Quartz.Impl;
using Serilog;
using Serilog.Core;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleCore
{
    class Program
    {
        private static Logger logger;
        static void Main(string[] args)
        {
            logger = LogHelper.CreateLoger();
            doa();
            while (true)
            {
                logger.Error($"秀明"+DateTime.Now.ToString("yyyyMMdd HHmmss"));
                Thread.Sleep(12 * 60 * 60 * 1000);
            }
        }
        public static async void doa()
        {
            Console.WriteLine("d");
            await Task.Run(() => CreateOrderJob());
            await Task.Run(() => CreateSendMsgJob());
            Console.WriteLine("d1");
        }
        /// <summary>
        /// 创建订单查询任务
        /// </summary>
        /// <returns></returns>
        public static async Task CreateOrderJob()
        {
            logger.Error("------- Initializing ----------------------");
            // First we must get a reference to a scheduler
            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sched = await sf.GetScheduler();
            logger.Error("------- Initialization Complete -----------");
            // computer a time that is on the next round minute
            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
            logger.Error("------- Scheduling Job  -------------------");
            // define the job and tie it to our HelloJob class
            IJobDetail job = JobBuilder.Create<OrderJob>()
                .WithIdentity("CreateOrderJob", "group1")
                .Build();
            // Trigger the job to run on the next round minute
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("CreateOrderJobtrigger1", "group1")
                .StartNow()
                 .WithSchedule(CronScheduleBuilder.CronSchedule("0 0 8,10,12,14,16 * * ?"))
                //.WithSchedule(CronScheduleBuilder.CronSchedule("0 0 18 * * ?"))//设置每天下午6点执行
                // .WithSchedule(CronScheduleBuilder.CronSchedule("0 0/58 8-18 * * ?"))//在每天上午8点到18点 58分钟触发一次
                //.WithSchedule(CronScheduleBuilder.CronSchedule("0 0/2 8-18 * * ?"))//在每天上午8点到18点 2分钟触发一次测试
                //.WithSimpleSchedule(x => x.WithIntervalInHours(12).RepeatForever())
                .Build();
            // Tell quartz to schedule the job using our trigger
            await sched.ScheduleJob(job, trigger);
            logger.Error($"{job.Key} will run at: {runTime:r}");
            // Start up the scheduler (nothing can actually run until the
            // scheduler has been started)
            await sched.Start();
            logger.Error("------- Started Scheduler -----------------");
        }
        /// <summary>
        /// 创建发送消息任务
        /// </summary>
        /// <returns></returns>
        public static async Task CreateSendMsgJob()
        {
            logger.Error("------- Initializing ----------------------");
            // First we must get a reference to a scheduler
            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sched = await sf.GetScheduler();
            logger.Error("------- Initialization Complete -----------");
            // computer a time that is on the next round minute
            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
            logger.Error("------- Scheduling Job  -------------------");
            // define the job and tie it to our HelloJob class
            IJobDetail job = JobBuilder.Create<SendMsgJob>()
                .WithIdentity("CreateSendMsgJob", "group1")
                .Build();
            // Trigger the job to run on the next round minute
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("CreateSendMsgJobtrigger1", "group1")
                .StartNow()
                //.WithSimpleSchedule(x => x.WithIntervalInHours(1).RepeatForever())
                //.WithSchedule(CronScheduleBuilder.CronSchedule("0 0 9 ? * MON-FRI"))//周一到周五每天9点运行
                .WithSchedule(CronScheduleBuilder.CronSchedule("0 0/59 11,17 * * ?"))//在每天上午8点和18点 59分钟触发一次
                                                                                    //.WithSchedule(CronScheduleBuilder.CronSchedule("0 0/3 8-18 * * ?"))//在每天上午8点到18点 3分钟触发一次测试
                .Build();
            // Tell quartz to schedule the job using our trigger
            await sched.ScheduleJob(job, trigger);
            logger.Error($"{job.Key} will run at: {runTime:r}");
            // Start up the scheduler (nothing can actually run until the
            // scheduler has been started)
            await sched.Start();
            logger.Error("------- Started Scheduler -----------------");
        }
    }
}
